package sg.edu.nus.iss.vmcs.interfaces;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import sg.edu.nus.iss.vmcs.customer.CoinInputCommand;
import sg.edu.nus.iss.vmcs.customer.CoinReceiver;
import sg.edu.nus.iss.vmcs.customer.CustomerUI;
import sg.edu.nus.iss.vmcs.system.MainController;
import sg.edu.nus.iss.vmcs.system.TransactionController;

public class SWTCustomerPanel extends Dialog implements CustomerUI{

	Object result;
	private TransactionController txnController;
	private MainController mainController;
	private SWTCustomerPanelListener btnListener;
	private Label copylabel;
	private VMCSCommandSWTAdapter vmcsAdapter;

	public SWTCustomerPanel(Shell parent, TransactionController tx) {
		super(parent);
		super.setText("VMCS Customer Panel");
		this.txnController = tx;
		vmcsAdapter = new VMCSCommandSWTAdapter(new CoinInputCommand((CoinReceiver)tx.getCoinReceiver()));
	}

	public TransactionController getTxnController() {
		return txnController;
	}

	public void setTxnController(TransactionController txnController) {
		this.txnController = txnController;
	}

	public MainController getMainController() {
		return mainController;
	}

	public void setMainController(MainController mainController) {
		this.mainController = mainController;
		setTxnController(mainController.getTrasactionController());
	}
	
	/**
	 * Sends the Weight of the coin to receive coin
	 * Weight is hard coded for demo purpose.In actual scenario it will be sent by the 
	 * weight transducer in actual vending machine.
	 * @return
	 */

	public Object open() {
		Shell parent = getParent();
		CoinReceiver receiver = (CoinReceiver) txnController.getCoinReceiver();
		btnListener = new SWTCustomerPanelListener(receiver);
		final Shell shell = new Shell(parent, SWT.DIALOG_TRIM
				| SWT.APPLICATION_MODAL);
		shell.setText(getText());
		shell.setLayout(new GridLayout(2, true));
		final Label enterCoin = new Label(shell, SWT.PUSH);
		enterCoin.setText("Enter Coin");
		enterCoin.setBounds(20, 20, 20, 20);
		{
		final Button button5c = new Button(shell, SWT.PUSH);
		button5c.setText("5C");
		button5c.setBounds(20, 40, 80, 25);
		button5c.addListener(SWT.Selection, vmcsAdapter);
		button5c.setData("Value", 7);
		}
		{
		final Button button10c = new Button(shell, SWT.PUSH);
		button10c.setText("10C");
		button10c.setBounds(20, 60, 80, 25);
		button10c.addListener(SWT.Selection, vmcsAdapter);
		button10c.setData("Value", 10);
		}
		{
		final Button button20c = new Button(shell, SWT.PUSH);
		button20c.setText("20C");
		button20c.setBounds(20, 80, 80, 25);
		button20c.addListener(SWT.Selection, vmcsAdapter);
		button20c.setData("Value", 18);
		}
		{
		final Button button50c = new Button(shell, SWT.PUSH);
		button50c.setText("50C");
		button50c.setBounds(20, 100, 80, 25);
		button50c.addListener(SWT.Selection, vmcsAdapter);
		button50c.setData("Value", 35);
		}
		{
		final Button button1D = new Button(shell, SWT.PUSH);
		button1D.setText("1D");
		button1D.setBounds(20, 120, 80, 25);
		button1D.addListener(SWT.Selection, vmcsAdapter);
		button1D.setData("Value", 42);
		}
		{
			final Button invalidbutton = new Button(shell, SWT.PUSH);
			invalidbutton.setText("Invalid");
			invalidbutton.setBounds(20, 140, 80, 25);
			invalidbutton.addListener(SWT.Selection, vmcsAdapter);
			invalidbutton.setData("Value", 0);
			}
		{
		final Label invalidCoinLabel = new Label(shell, SWT.PUSH);
		Color color = new Color(getParent().getDisplay(), 255,255,255);
		invalidCoinLabel.setBackground(color);
		invalidCoinLabel.setText("Invalid coin");
		invalidCoinLabel.setBounds(20, 160, 80, 25);
		copylabel = invalidCoinLabel;
		}
		shell.open();
		
		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	@Override
	public void display() {
		open();
	}

	@Override
	public void closeDown() {
	}

	@Override
	public void allowDrinksSelection(boolean allowSelection) {
	}

	@Override
	public void setInvalidLabel(boolean state) {
		Color color = new Color(getParent().getDisplay(), 255,255,255);
		copylabel.setBackground(color);
		System.out.println("SWT Panel====>"+"Invalid Coin Entered");
	}
}
